﻿using LiteStep.Currencies;
using LiteStep.Exchanges;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteStep.Conversions
{
	public class SimpleConversion<TFrom, TTo, TOn> : IConversion<TFrom, TTo, TOn>
		where TFrom : ICurrency<TFrom>, new()
		where TTo : ICurrency<TTo>, new()
		where TOn : IExchange, new()
	{
		public decimal ConversionRatio { get; private set; }
        public decimal Volume { get; private set; }
		public DateTime Timestamp { get; private set; }
		public TFrom From { get; private set; }
		public TTo To { get; private set; }
		public TOn Exchange { get; private set; }

		public SimpleConversion(decimal conversionRatio, DateTime timestamp, decimal volume = 0.0M)
		{
			if (conversionRatio <= 0.0M)
				throw new ArgumentOutOfRangeException("conversionRatio", "conversionRatio must be > 0");
			this.ConversionRatio = conversionRatio;
            this.Volume = volume;
			this.Timestamp = timestamp;

			this.From = (new TFrom()).Create(volume);
			this.To = new TTo();
			this.Exchange = new TOn();
		}

        public TTo Convert()
        {
            return Convert(From);
        }

		public TTo Convert(TFrom from)
		{
			if (from == null)
				throw new ArgumentNullException("from", "the from currency cannot be null");

			return (new TTo()).Create(from.Amount * this.ConversionRatio);
		}
    }
}
